/*
 * @Author: gitee_com_zb
 * @Date: 2024-11-15 09:32:37
 * @LastEditors: gitee_com_zb
 * @LastEditTime: 2024-11-15 09:32:39
 * @FilePath: /lzb/GiteeCode/algorithm/优选算法41.外观数列(medium).cpp
 * @Description: 题目链接 https://leetcode.cn/problems/count-and-say
 */
class Solution {
public:
    string countAndSay(int n) {
        // 模拟+双指针
        string ret = "1";
        for(int i = 1; i < n; i++) { // 翻译n-1次
            string temp;
            for(int left = 0, right = 0; right < ret.size();) {
                while(right < ret.size() && ret[left] == ret[right]) right++;
                temp += to_string(right - left) + ret[left]; 
                left = right;
            }
            ret = temp;
        }
        return ret;
    }
};